package com.example.handheld4_gsignal_app.services;

import android.content.Context;
import android.location.Location;
import com.example.handheld4_gsignal_app.data.models.LocationData;
import com.example.handheld4_gsignal_app.data.models.NetworkData;
import com.example.handheld4_gsignal_app.data.models.SignalCollectionData;
import java.util.Date;

/**
 * 信号采集服务
 */
public class SignalCollectionService {
    private Context context;
    private LocationService locationService;
    private NetworkService networkService;
    private String deviceId;
    private boolean isCollecting = false;

    public interface CollectionCallback {
        void onCollectionSuccess(SignalCollectionData data);
        void onCollectionError(String error);
        void onLocationUpdate(LocationData locationData);
        void onNetworkUpdate(NetworkData networkData);
        void onSignalStrengthUpdate(int signalStrength);
    }

    public SignalCollectionService(Context context) {
        this.context = context;
        this.locationService = new LocationService(context);
        this.networkService = new NetworkService(context);
        this.deviceId = android.provider.Settings.Secure.getString(
            context.getContentResolver(), 
            android.provider.Settings.Secure.ANDROID_ID
        );
    }

    /**
     * 开始信号采集
     */
    public void startCollection(CollectionCallback callback) {
        if (isCollecting) {
            if (callback != null) {
                callback.onCollectionError("采集已在进行中");
            }
            return;
        }

        isCollecting = true;

        // 启动位置服务
        locationService.startLocationUpdates(new LocationService.LocationCallback() {
            @Override
            public void onLocationUpdate(Location location) {
                if (callback != null) {
                    // 将Location转换为LocationData
                    LocationData locationData = new LocationData(
                        location.getLatitude(),
                        location.getLongitude(),
                        location.hasAltitude() ? location.getAltitude() : null,
                        location.hasAccuracy() ? (double) location.getAccuracy() : null,
                        new Date(location.getTime())
                    );
                    callback.onLocationUpdate(locationData);
                }
            }

            @Override
            public void onLocationError(String error) {
                if (callback != null) {
                    callback.onCollectionError("位置服务错误: " + error);
                }
            }
        });

        // 启动网络服务
        networkService.startNetworkMonitoring(new NetworkService.NetworkCallback() {
            @Override
            public void onNetworkInfoUpdate(NetworkData networkData) {
                if (callback != null) {
                    callback.onNetworkUpdate(networkData);
                }
            }

            @Override
            public void onSignalStrengthUpdate(int signalStrength) {
                if (callback != null) {
                    callback.onSignalStrengthUpdate(signalStrength);
                }
            }

            @Override
            public void onNetworkError(String error) {
                if (callback != null) {
                    callback.onCollectionError("网络服务错误: " + error);
                }
            }
        });
    }

    /**
     * 停止信号采集
     */
    public void stopCollection() {
        if (isCollecting) {
            locationService.stopLocationUpdates();
            networkService.stopNetworkMonitoring();
            isCollecting = false;
        }
    }

    /**
     * 执行单次信号采集
     */
    public void collectSignal(LocationData locationData, NetworkData networkData, 
                             int signalStrength, CollectionCallback callback) {
        try {
            SignalCollectionData data = new SignalCollectionData();
            data.setDeviceId(deviceId);
            data.setSignalStrength(signalStrength);
            data.setCollectedAt(new Date());

            if (locationData != null) {
                data.setLatitude(locationData.getLatitude());
                data.setLongitude(locationData.getLongitude());
                data.setAltitude(locationData.getAltitude());
                data.setAccuracy(locationData.getAccuracy());
            }

            if (networkData != null) {
                data.setNetworkType(networkData.getNetworkType());
                data.setOperatorName(networkData.getOperatorName());
                data.setCellId(networkData.getCellId());
                data.setLac(networkData.getLac());
                data.setMcc(networkData.getMcc());
                data.setMnc(networkData.getMnc());
                data.setRsrp(networkData.getRsrp());
                data.setRsrq(networkData.getRsrq());
                data.setRssi(networkData.getRssi());
                data.setSnr(networkData.getSnr());
            }

            if (callback != null) {
                callback.onCollectionSuccess(data);
            }
        } catch (Exception e) {
            if (callback != null) {
                callback.onCollectionError("信号采集失败: " + e.getMessage());
            }
        }
    }

    /**
     * 获取设备ID
     */
    public String getDeviceId() {
        return deviceId;
    }

    /**
     * 检查是否正在采集
     */
    public boolean isCollecting() {
        return isCollecting;
    }

    /**
     * 获取最后一次已知位置
     */
    public LocationData getLastKnownLocation() {
        Location location = locationService.getLastKnownLocation();
        if (location != null) {
            return new LocationData(
                location.getLatitude(),
                location.getLongitude(),
                location.hasAltitude() ? location.getAltitude() : null,
                location.hasAccuracy() ? (double) location.getAccuracy() : null,
                new Date(location.getTime())
            );
        }
        return null;
    }

    /**
     * 检查位置服务是否可用
     */
    public boolean isLocationEnabled() {
        // 简单检查位置服务是否可用
        return locationService != null;
    }

    /**
     * 检查网络连接状态
     */
    public boolean isNetworkConnected() {
        return networkService.isNetworkConnected();
    }
}